Smart ImportThis script allows the user to import the full, nested contents of a folder just by selecting it. It attempts to detect whether each item is a still, moving footage, or an image sequence. The user still has to make other choices via dialogs, such as which layer of a multi-layer image (e.g. a .psd) to import. First, we prompt the user for a folder whose contents are to be imported, and ascertain that the user chooses a folder rather than cancelling the dialog. We then call a function which appears below to import all of the files, one by one. var targetFolder = folderGetDialog("Import Items from Folder..."); //returns a folder or null if (targetFolder) { function processFile (theFile) { var importOptions = new ImportOptions (theFile); //create a variable containing ImportOptions importSafeWithError (importOptions); } Now we add a function to test whether a given file is part of a sequence. This uses Regular Expressions, which are a special type of JavaScript designed to reduce the number of steps required to evaluate a string. The firstone tests for the presence of sequential numbers anywhere in the file name, followed by another making certain that the sequential files aren't of a type that can't be imported as a sequence (moving image files). We then check adjacent files to see if a sequence exists, stopping after we've evaluated ten files to save processing time. function testForSequence (files){ var searcher = new RegExp ("[0-9]+"); var movieFileSearcher = new RegExp ("(mov|avi|mpg)$", "i"); var parseResults = new Array; for (x = 0; (x < files.length) & x < 10; x++) { //test that we have a sequence, stop parsing after 10 files var movieFileResult = movieFileSearcher.exec(files[x].name); if (! movieFileResult) { var currentResult = searcher.exec(files[x].name); If no match is found using the Regular Expression looking for a number string, we get null and assume there is no image sequence. Otherwise, we want an array consisting of the matched string and its location within the file name. if (currentResult) { //we have a match - the string contains numbers //the match of those numbers is stored in the array[1] //take that number and save it into parseResults parseResults[parseResults.length] = currentResult[0]; } else { parseResults[parseResults.length] = null; } } else { parseResults[parseResults.length] = null; } } Now if all of the files just evaluated indicated that they are part of a numbered sequence, we assume that we have a sequence and return the first file of that sequence. Otherwise, we end this function. var result = null; for (i = 0; i < parseResults.length; ++i) { if (parseResults[i]) { if (! result) { result = files[i]; } } else { //case in which a file name did not contain a number result = null; break; } } return result; } Next we add a function to pop up error dialogs if there is a problem with any file we are attempting to import. function importSafeWithError (importOptions) { try { app.project.importFile (importOptions); } catch (error) { alert(error.toString() + importOptions.file.fsName); } } Next comes a function to actually import any image sequence that we discover using testForSequence(), above. Note that there is an option for forcing alphabetical order in sequences, which is commented out in the script as written. If you want to force alphabetical order, un-comment the line which reads, "importOptions.forceAlphabetical = true" by removing the two slashes at the beginning of that line. function processFolder(theFolder) { var files = theFolder.getFiles(); //Get an array of files in the target folder //test whether theFolder contains a sequence var sequenceStartFile = testForSequence(files); //if it does contain a sequence, import the sequence if (sequenceStartFile) { var importOptions = new ImportOptions (sequenceStartFile); //create a variable containing ImportOptions importOptions.sequence = true; //importOptions.forceAlphabetical = true; //un-comment this if you want to force alpha order by default importSafeWithError (importOptions); } //otherwise, import the files and recurse for (index in files) { //Go through the array, set each element to singleFile, run this: if (files[index] instanceof File) { if (! sequenceStartFile) { //if file is already part of a sequence, don't import it individually processFile (files[index]); //calls the processFile function above } } if (files[index] instanceof Folder) { processFolder (files[index]); // recursion } } } processFolder(targetFolder); } |